package tw.arduino.view;

import java.net.URL;
import java.util.TimerTask;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Text;
import com.swtdesigner.SWTResourceManager;
/**
 * @ArduinoTW Command Center
 * @author XLINX x
 * @since 2010.08.08
 * @see http://arduino.tw
 */

public class SWTBrowserComposite extends Composite {
	public static final String HOME_URL = "http://arduino.tw";
	private Text locationText;
	public static Browser browser;
	public static Long e_chiang;

	/**
	 * Create the composite
	 * 
	 * @param parent
	 * @param style
	 */

	public static void setURL(String t, String url) {
		System.err.println("[SWTBrowser][SWT][BROWSER]" + url);
		browser.setUrl(url);
	}

	public SWTBrowserComposite(Composite parent, int style) {
		super(parent, style);

		final GridLayout gridLayout = new GridLayout();
		gridLayout.numColumns = 7;
		setLayout(gridLayout);

		final Button backButton = new Button(this, SWT.NONE);
		backButton.setImage(SWTResourceManager.getImage(SWTBrowserComposite.class, "/images/gif/correction_change2.gif"));

		final Button forwardButton = new Button(this, SWT.NONE);
		forwardButton.setImage(SWTResourceManager.getImage(SWTBrowserComposite.class, "/images/gif/correction_change.gif"));

		final Button stopButton = new Button(this, SWT.NONE);
		stopButton.setImage(SWTResourceManager.getImage(SWTBrowserComposite.class, "/images/gif/delete.gif"));
		final GridData gd_stopButton = new GridData();
		gd_stopButton.minimumHeight = 30;
		gd_stopButton.minimumWidth = 30;
		stopButton.setLayoutData(gd_stopButton);
		stopButton.setText("Stop");

		final Button refreshButton = new Button(this, SWT.NONE);
		refreshButton.setImage(SWTResourceManager.getImage(SWTBrowserComposite.class, "/images/gif/refresh.gif"));
		final GridData gd_refreshButton = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		gd_refreshButton.minimumHeight = 30;
		gd_refreshButton.minimumWidth = 30;
		refreshButton.setLayoutData(gd_refreshButton);
		refreshButton.setText("Refresh");

		final Button homeButton = new Button(this, SWT.NONE);
		homeButton.setImage(SWTResourceManager.getImage(SWTBrowserComposite.class, "/images/gif/home_nav.gif"));
		final GridData gd_homeButton = new GridData(SWT.CENTER, SWT.CENTER, false, false);
		gd_homeButton.minimumHeight = 30;
		gd_homeButton.minimumWidth = 30;
		homeButton.setLayoutData(gd_homeButton);
		homeButton.setText("Home");

		locationText = new Text(this, SWT.BORDER);
		locationText.setText("http://arduino.tw");
		locationText.setFont(SWTResourceManager.getFont("Verdana", 20, SWT.NONE));
		locationText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));

		final Button goButton = new Button(this, SWT.NONE);
		goButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, false, false));
		goButton.setImage(SWTResourceManager.getImage(SWTBrowserComposite.class, "/images/gif/osprc_obj.gif"));
		goButton.setText("GO");

		browser = new Browser(this, SWT.BORDER);
		final GridData gd_browser = new GridData(SWT.FILL, SWT.FILL, true, true, 7, 1);
		gd_browser.widthHint = 262;
		browser.setLayoutData(gd_browser);
		browser.setUrl("http://guide.cc.ntu.edu.tw/ntugis/gis_map.htm");

		final Label statusText = new Label(this, SWT.BORDER);
		statusText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, false, false, 6, 1));
		statusText.setText("ArduinoTW Command Center Browse Loading...OK!!");
		locationText.addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseUp(org.eclipse.swt.events.MouseEvent e) {
				locationText.selectAll();
			}
		});
		final ProgressBar progressBar = new ProgressBar(this, SWT.NONE);
		final GridData gd_progressBar = new GridData(SWT.CENTER, SWT.FILL, false, false, 1, 1);
		gd_progressBar.widthHint = 73;
		progressBar.setLayoutData(gd_progressBar);

		locationText.addKeyListener(new org.eclipse.swt.events.KeyAdapter() {
			public void keyPressed(org.eclipse.swt.events.KeyEvent e) {
				// Handle the press of the Enter key in the locationText.
				// This will browse to the entered text.
				if (e.character == SWT.LF || e.character == SWT.CR) {
					e.doit = false;
					browser.setUrl(locationText.getText());
				}
			}
		});
		refreshButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				browser.refresh();
			}
		});
		locationText.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				browser.setUrl(locationText.getText());
			}
		});
		stopButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				browser.stop();
			}
		});
		homeButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				browser.setUrl(HOME_URL);
			}
		});
		goButton.addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				browser.setUrl(locationText.getText());
			}
		});
		browser.addTitleListener(new org.eclipse.swt.browser.TitleListener() {
			public void changed(org.eclipse.swt.browser.TitleEvent e) {
				// sShell.setText(APP_TITLE + " - " + e.title);
			}
		});
		browser.addLocationListener(new org.eclipse.swt.browser.LocationListener() {
			public void changing(org.eclipse.swt.browser.LocationEvent e) {
				locationText.setText(e.location);
			}

			public void changed(org.eclipse.swt.browser.LocationEvent e) {
			}
		});
		browser.addProgressListener(new org.eclipse.swt.browser.ProgressListener() {
			public void changed(org.eclipse.swt.browser.ProgressEvent e) {
				if (!stopButton.isEnabled() && e.total != e.current) {
					stopButton.setEnabled(true);
				}
				progressBar.setMaximum(e.total);
				progressBar.setSelection(e.current);
			}

			public void completed(org.eclipse.swt.browser.ProgressEvent e) {
				stopButton.setEnabled(false);
				backButton.setEnabled(browser.isBackEnabled());
				forwardButton.setEnabled(browser.isForwardEnabled());
				progressBar.setSelection(0);
			}
		});
		browser.addStatusTextListener(new org.eclipse.swt.browser.StatusTextListener() {
			public void changed(org.eclipse.swt.browser.StatusTextEvent e) {
				statusText.setText(e.text);
			}
		});
		browser.setUrl(HOME_URL);
		
		//
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

}
